<%@ page language="java" import="java.util.*" import="java.text.SimpleDateFormat" pageEncoding="UTF-8"%>
<%@ page import="com.bluesky.light.user.*" %>
<%@ page import="com.bluesky.light.order.*" %>
<%
	request.setCharacterEncoding("UTF-8");
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	// check if the user is login
	User u = (User)session.getAttribute("user");
	if (u == null) {
		response.sendRedirect("index.jsp");
	}
	String name = u.getName();
	
	Date today = new Date();
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(today);
	
	//get year, month, day and monthdays
	int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
	int monthDays = calendar.getActualMaximum(Calendar.DATE);
	int month = calendar.get(Calendar.MONTH) + 1;
	int year = calendar.get(Calendar.YEAR);
	
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>light - home</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link href="css/main.css" rel="stylesheet" type="text/css" />

  </head>
  <body>
	<!-- header -->
	<div id="header">
		<div id="logo"><img src="images/logo_small2.png"/></div>
	</div>
	<!-- // header -->
	<!-- nav -->
	<div id="nav">
	</div>
	<!-- // nav -->
	<!-- container -->
	<div id="container">
		<!-- contents -->
		<div id="contents">
	    	<div id="menu">
	        	<div>
	        		<div id="user_info">
	            		<img src="develop/user_icon.jpg"/> 
	            		<b>User</b>
	           		</div>
	           		<div>
	           		<%=name%>
	           		</div>
	            </div>
	            <div>
	            	<%SimpleDateFormat sdf = new SimpleDateFormat("EEE, yyyy-MM-dd", new Locale("US"));%><%=sdf.format(new Date())%><br></br>
	            </div>	
	        	<ul>
	            	<li><a href="home.jsp"><img src="images/news.png" /> Health News</a></li>
	                <li><a href="user_subscribe.jsp"><img src="images/surscribe.png" /> Appointment</a></li>
	                <li><a href="user_history.jsp"><img src="images/account.png" /> Health History</a></li>
	                <li><a href="user_hospitalSearchOnMap.jsp"><img src="images/photo.png" /> Searching Hospital</a></li>
	                <li><a href="user_bodyCheckResult.jsp"><img src="images/surscribe.png" /> Body Check Result</a></li>
	                <li><a href="user_infoModifyInput.jsp"><img src="images/account.png" /> Account Management</a></li>	    
	                <li><a href="index.jsp"><img src="images/account.png" /> Logout</a></li>
	            </ul>
	        </div>
	        <div id="main-cont">
	            <div id="result">
	                <table class="resulttable" rules="all" >
	                	<caption>OutPatient Date Choose</caption>
	                	<tr><th colspan="7"><%=month%>月</th></tr>
	                	<tr>
						<%
							for (int i = 0; i < monthDays; i++) {
								//create a date to check if it is full
								calendar.set(year, month-1, i+1);
								Date opdate = calendar.getTime();
						%>
							<% 	if (i % 7 == 0) { %> </tr><tr> <% } %>
							<% 	if (i < dayOfMonth - 1) { // TODO decase 1 is for test system%>
									<td><%=i+1%></td>
							<%  } else if (OrderService.getInstance().getBookCount(opdate) > 5) {%>
									<td><%=i+1%></td>
							<% 	} else { %>
									<!-- <td><a href="user_outpatientBook.jsp?action=outpatient&oyear=<%=year%>&omonth=<%=month%>&oday=<%=i+1%>"><%=i+1%></a></td>-->
									<td><a href="user_outpatientBook.jsp?action=outpatient&oyear=<%=year%>&omonth=<%=month%>&oday=<%=i+1%>"><%=i+1%></a></td>
						<%		
								}
							}
						%>
						</tr>
	                </table>		
	        	</div>	
	        	<%
	        		// reset day
	        		calendar.setTime(today);
	        		// set next month to calendar and get monthdays
	        		calendar.set(Calendar.YEAR, Calendar.MONTH, 1);
	        		int nextMonthDays = calendar.getActualMaximum(Calendar.DATE);
	        		// set month
	        		month = month % 12 + 1;
	        		// set year if it over a year
	        		if (month == 1) {
	        			year++;
	        		}
	        	%>
	            <div id="result">
	                <table class="resulttable" rules="all" >
	                	<tr><th colspan="7"><%=month%>月</th></tr>
	                	<tr>
						<%
							for (int i = 0; i < nextMonthDays; i++) {
								calendar.set(year, month-1, i+1);
								Date opdate = calendar.getTime();								
						%>
							<% 	if (i % 7 == 0) { %> </tr><tr> <% } %>
							<%  if (OrderService.getInstance().getBookCount(opdate) > 5) {%>
								<td><%=i+1%></td>		
							<%  } else { %>					
								<td><a href="user_outpatientSuccessfulBooking.jsp?action=outpatient&oyear=<%=year%>&omonth=<%=month%>&oday=<%=i+1%>"><%=i+1%></a></td>
						<%	
								}
							}
						%>
						</tr>
	                </table>		
	        	</div>				
	        </div>
	    </div>
	    <!-- // contents -->
	</div>
	<!-- // container -->
	<div id="footer">
		<div id="bs_stdio">
	    	©2011-2011 www.bluesky.com.mo All Rights Reserved<br/>
	    	<img src="images/bluesky_logo.png" />
	    </div>
	</div>
  </body>
</html>
